package user

import (
	"github.com/gin-gonic/gin"
	"management/database"
	"management/jwt"
	"management/middleware"
	"management/util"
	"strconv"
)

type User struct {
	Name string `json:"name"`
	Id   string `json:"id"`
}

func InitUser(g *gin.Engine) {
	router := g.Group("/user")
	router.Use(middleware.AuthTokenMiddleware(jwt.MySigningKey))
	// 更新用户信息
	router.PUT("/updateUser", func(c *gin.Context) {

	})
	// 冻结用户
	router.PUT("/frozenUser/:id", func(c *gin.Context) {
		id := c.Param("id")
		status := c.PostForm("status")
		if status != "0" && status != "1" {
			util.Error(c, &util.Result{Data: nil, Msg: "状态未能识别，请重试！"})
			return
		}
		err := database.FrozenUser(&database.User{Id: id, Status: status})
		if err != nil {
			util.Error(c, &util.Result{Data: nil, Msg: err.Error()})
			return
		}
		util.Success(c, &util.Result{Data: nil, Msg: "冻结用户成功！"})
	})
	// 获取用户列表
	router.GET("/getUserList", func(c *gin.Context) {
		name := c.Query("name")
		pageIndex, _ := strconv.Atoi(c.Query("pageIndex"))
		pageSize, _ := strconv.Atoi(c.Query("pageSize"))
		users, total := database.GetUserList(&database.UserPagination{
			PageIndex: pageIndex,
			PageSize:  pageSize,
			Name:      name,
		})
		util.Success(c, &util.Result{
			Data: gin.H{
				"users":     users,
				"total":     total,
				"pageIndex": pageIndex,
				"pageSize":  pageSize,
			},
			Msg: "获取用户列表成功",
		})
	})
}
